/**   
 * 项目名：五位一体化工安全信息平台
 * 日期：    2019-12-08 15:07:44  
 * Copyright (c) 2015- joysuch-版权所有     
 */
package com.joysuch.wwyt.bp.service.impl;

import javax.transaction.Transactional;


import com.joysuch.wwyt.bp.bean.BpContractorActivitiesScopeDetailBean;
import com.joysuch.wwyt.bp.bean.BpContractorActivitiesScopeEditBean;
import com.joysuch.wwyt.bp.bean.BpContractorActivitiesScopeListBean;
import static com.joysuch.wwyt.bp.bean.BpContractorActivitiesScopeListBean.Search;

import com.joysuch.wwyt.core.common.util.PageDataUtils;
import com.joysuch.wwyt.core.common.util.PageDataUtils.PageDataConvert;
import com.joysuch.wwyt.core.exception.BusinessRestrictionsException;
import com.joysuch.wwyt.core.service.BizCodeGeneratorFacade;

import com.querydsl.core.types.Predicate;
import com.joysuch.wwyt.bp.entity.BpContractorActivitiesScope;
import com.joysuch.wwyt.bp.repository.BpContractorActivitiesScopeDao;
import com.joysuch.wwyt.bp.service.BpContractorActivitiesScopeService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

@Service
@Transactional
public class BpContractorActivitiesScopeServiceImpl implements BpContractorActivitiesScopeService {

    @Autowired
    private BpContractorActivitiesScopeDao dao;
    @Autowired
    private BizCodeGeneratorFacade bizCodeGeneratorFacade;

    @Override
    public BpContractorActivitiesScope add(BpContractorActivitiesScopeEditBean entity) throws Exception {
        BpContractorActivitiesScope content  = new BpContractorActivitiesScope();
        //单位ID
        content.setCompanyId(entity.getCompanyId());
        //区域ID
        content.setPodAreaId(entity.getPodAreaId());
        return dao.save(content);
    }

    @Override
    public void update(BpContractorActivitiesScopeEditBean entity) throws Exception {
        BpContractorActivitiesScope content = dao.findById(entity.getId()).get();
        //ID，主键
        content.setId(entity.getId());
        //单位ID
        content.setCompanyId(entity.getCompanyId());
        //区域ID
        content.setPodAreaId(entity.getPodAreaId());
        dao.save(content);
    }

    @Override
    public Page<BpContractorActivitiesScopeListBean> findByPage(Pageable page , Search condition) {
        Predicate predicate = prepareListSearchCondition(condition);
        
        Page<BpContractorActivitiesScope> data =  predicate == null? dao.findAll(page) : dao.findAll(predicate, page);
        
        return PageDataUtils.convertPageData(data, new PageDataConvert<BpContractorActivitiesScopeListBean, BpContractorActivitiesScope>() {

            @Override
            public BpContractorActivitiesScopeListBean convert(BpContractorActivitiesScope f) {
                BpContractorActivitiesScopeListBean bean = new BpContractorActivitiesScopeListBean();
                
                //ID，主键
                 bean.setId(f.getId());
                //单位ID
                 bean.setCompanyId(f.getCompanyId());
                //区域ID
                 bean.setPodAreaId(f.getPodAreaId());
                
                return bean;
            }

        });
    }

    private Predicate  prepareListSearchCondition(Search condition) {
        return null;
    }
    @Override
    public void delete(Long id) throws BusinessRestrictionsException {
        //校验业务逻辑是否允许删除。如本数据被其他业务关联，删除后导致其他业务不可用。不能删除时将抛出业务异常
        checkBussinessRestrictions(id);
        dao.deleteById(id);
    }
    
    /**
     * 校验业务逻辑是否允许删除。如本数据被其他业务关联，删除后导致其他业务不可用
     * @param id 要删除的业务ID
     * @throws BusinessRestrictionsException 抛出的业务异常
     */
    private void checkBussinessRestrictions(Long id) throws BusinessRestrictionsException {
        
    }
    
    @Override
    public BpContractorActivitiesScope findById(Long id) {
        return dao.findById(id).get();
    }
    
    @Override
    public BpContractorActivitiesScopeDetailBean getById(Long id) {
        BpContractorActivitiesScope entity = findById(id) ;
        BpContractorActivitiesScopeDetailBean bean = new BpContractorActivitiesScopeDetailBean();
        //ID，主键
        bean.setId(entity.getId());
        
        //单位ID
        bean.setCompanyId(entity.getCompanyId());
        //bean.setCompanyName(entity.getCompanyName());
        
        //区域ID
        bean.setPodAreaId(entity.getPodAreaId());
        //bean.setPodAreaName(entity.getPodAreaName());
        
        return bean;
    }

}
